import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController } from 'ionic-angular';
import { PAGE_COUNT } from '../../../app/app.constants';
import { ApiProvider } from '../../../providers/api/api';
import { NotificationProvider } from '../../../providers/notification/notification';

/**
 * Generated class for the StoreWarnPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-store-warn',
  templateUrl: 'store-warn.html',
})
export class StoreWarnPage {
  private funid = 'sp_limit_house';
  private items: Array<string>;
  private searchValue: string;
  private options = {
    start: 0,
    limit: PAGE_COUNT,
    where_sql:'v_sp_store_house.store_num >sp_limit.store_top or v_sp_store_house.store_num < sp_limit.store_lower ',
    where_type:'',
    where_value:''
  };
 // private warn_num:any;
  private searchTimer: any;

  constructor(
    public navCtrl: NavController, 
    public navParams: NavParams, 
    private api: ApiProvider, 
    private notification: NotificationProvider,
    public modalCtrl: ModalController) {}

  ionViewDidLoad() {
    this.notification.showLoading();
    //this.warn_num = 0;
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        this.items = res['data']['root'];
       // this.warn_num=this.items.length;
      }else{
        this.notification.showAlert(res['message']); 
      } 
      this.notification.hideLoading(); 
    })
  }
  
  /**
   * 
   * @param store 查看详情
   * @param index 
   */
  showDetail(item){ 
    this.navCtrl.push('WarnDetailPage', {'store_house':item});
  }

  /**
   * 搜索存货
   */
  getItems(e){
    clearTimeout(this.searchTimer);
    this.searchTimer = setTimeout(() => {
      this.options.where_sql="v_sp_store_house__sp_name like ?";
      this.options.where_type="string";
      this.options.where_value='%'+this.searchValue+'%';
      this.doSearch();
    }, 1000);
  }

  /**
   * 执行搜索
   */
  doSearch(){
    this.notification.showLoading();
    this.options.start=0;
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        this.items = res['data']['root'];
      }else{
        this.notification.showAlert(res['message']); 
      } 
      this.notification.hideLoading();
    })
  }

  /**
   * 下拉刷新
   * @param refresher 
   */
  doRefresh(refresher) {
    this.options.start=0;
    
    let res = this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        this.items = res['data']['root'];
      }else{
        this.notification.showAlert(res['message']); 
      } 
      refresher.complete();
    })
    
  }

  /**
   * 上拉加载
   * @param infiniteScroll 
   */
  doInfinite(infiniteScroll) {
    console.log('Begin async operation');
    this.options.start += PAGE_COUNT;
    this.api.getListPage(this.funid,this.options).subscribe(res => {
      if(res['success']){
        let newItems = res['data']['root'];
        newItems.forEach(item => {
          this.items.push(item);
        });
      }else{
        this.notification.showAlert(res['message']); 
      }
    infiniteScroll.complete();
    });
  } 

}
